package com.example.desinger.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.desinger.common.R;
import com.example.desinger.designPattern.dutymodel.Handler;
import com.example.desinger.designPattern.dutymodel.HomeworkExistenceHandler;
import com.example.desinger.designPattern.dutymodel.SaveRemarkHandler;
import com.example.desinger.designPattern.dutymodel.UpdateHomeworkScoreHandler;
import com.example.desinger.entity.HomeRemark;
import com.example.desinger.mapper.HomeRemarkMapper;
import com.example.desinger.service.HomeRemarkService;
import com.example.desinger.service.StuHomeWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



@Service
public class HomeRemarkImpl extends ServiceImpl<HomeRemarkMapper, HomeRemark> implements HomeRemarkService {
    private Handler handlerChain;

    public void HomeRemarkImpl(StuHomeWorkService stuHomeWorkService) {
        // 构建处理器链
        Handler homeworkExistenceHandler = new HomeworkExistenceHandler(stuHomeWorkService);
        Handler saveRemarkHandler = new SaveRemarkHandler();
        Handler updateHomeworkScoreHandler = new UpdateHomeworkScoreHandler(stuHomeWorkService);

        homeworkExistenceHandler.setNext(saveRemarkHandler);
        saveRemarkHandler.setNext(updateHomeworkScoreHandler);

        this.handlerChain = homeworkExistenceHandler;
    }

    @Autowired
   private StuHomeWorkService stuHomeWorkService;

    //学生评论学生作业
    @Override
    public R<String> remarkStudentOfHW(HomeRemark homeRemark) {
        //判断作业是否存在
//        LambdaQueryWrapper<StuHomeWork> lambdaQueryWrapper =
//                new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(StuHomeWork::getClassId,homeRemark.getClass_id())
//                .eq(StuHomeWork::getStuId,homeRemark.getStu_id())
//                .eq(StuHomeWork::getId,homeRemark.getStu_home_work_id());
//        StuHomeWork one = stuHomeWorkService.getOne(lambdaQueryWrapper);
//        if (one==null){
//            return R.error("数据库中没有这个同学的作业");
//        }
//        //添加表，HR中自己的方法
//         boolean save = save(homeRemark);
//
//        if (save){
//            LambdaUpdateWrapper<StuHomeWork> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
//            lambdaUpdateWrapper.eq(StuHomeWork::getClassId,homeRemark.getClass_id())
//                    .eq(StuHomeWork::getStuId,homeRemark.getStu_id())
//                    .eq(StuHomeWork::getHomeWorkId,homeRemark.getStu_home_work_id());
//            StuHomeWork stuHomeWork = new StuHomeWork();
//            stuHomeWork.setScore(2);
//
//            boolean update = stuHomeWorkService.update(stuHomeWork, lambdaUpdateWrapper);
//            if (update){
//                return R.success("添加评论信息成功");
//            }
//        }
//        return R.error("未知错误");
        HomeRemarkImpl(stuHomeWorkService);
        return handlerChain.handle(homeRemark);
    }






}
